//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package chat_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_chat_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type ChatGroupInfoDbItem struct {
	ChatGroupId string `bson:"_id"`
	ProjectId   string `bson:"projectId"`
	Title       string `bson:"title"`
	System      bool   `bson:"system"`
	OwnerUserId string `bson:"ownerUserId"`
	LastMsgId   string `bson:"lastMsgId"`
	CreateTime  int64  `bson:"createTime"`
}

func (item *ChatGroupInfoDbItem) ToChatGroupInfo(userPerm *project_chat_api.UserPerm, unreadCount uint32) *project_chat_api.ChatGroupInfo {
	return &project_chat_api.ChatGroupInfo{
		ChatGroupId: item.ChatGroupId,
		Title:       item.Title,
		System:      item.System,
		OwnerUserId: item.OwnerUserId,
		UserPerm:    userPerm,
		UnreadCount: unreadCount,
		SortValue:   item.CreateTime,
	}
}

type _GroupDao struct {
	mc *mongo.Collection
}

func newGroupDao(db *mongo.Database) (*_GroupDao, error) {
	mc := db.Collection(db_ns.DB_NS_CHAT + "group")
	return &_GroupDao{mc: mc}, nil
}

func (db *_GroupDao) Insert(ctx context.Context, dbItem *ChatGroupInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_GroupDao) Exist(ctx context.Context, projectId, chatGroupId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": chatGroupId, "projectId": projectId})
	return count > 0, err
}

func (db *_GroupDao) Get(ctx context.Context, projectId, chatGroupId string) (*ChatGroupInfoDbItem, error) {
	retItem := &ChatGroupInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": chatGroupId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_GroupDao) Remove(ctx context.Context, projectId, chatGroupId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": chatGroupId, "projectId": projectId})
	return err
}

func (db *_GroupDao) ListById(ctx context.Context, projectId string, chatGroupIdList []string) ([]*ChatGroupInfoDbItem, error) {
	if len(chatGroupIdList) == 0 {
		return []*ChatGroupInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": chatGroupIdList}, "projectId": projectId})
	if err != nil {
		return nil, err
	}
	retList := []*ChatGroupInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_GroupDao) UpdateTitle(ctx context.Context, projectId, chatGroupId, title string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": chatGroupId, "projectId": projectId}, bson.M{"$set": bson.M{"title": title}})
	return err
}

func (db *_GroupDao) UpdateOwnerUserId(ctx context.Context, projectId, chatGroupId, ownerUserId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": chatGroupId, "projectId": projectId}, bson.M{"$set": bson.M{"ownerUserId": ownerUserId}})
	return err
}

func (db *_GroupDao) UpdateLastMsgId(ctx context.Context, projectId, chatGroupId, lastMsgId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": chatGroupId, "projectId": projectId}, bson.M{"$set": bson.M{"lastMsgId": lastMsgId}})
	return err
}
